﻿
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Ledict.ConditionExtract.BLL;
using Ledict.ConditionExtract.Common;
using Ledict.ConditionExtract.Model;
using System.Threading;
namespace Ledict.ConditionExtract
{
    public partial class WB_Manage_Base : Form
    {
        public WB_Manage_Base()
        {
            InitializeComponent();

        }


        static WB_Manage_Base _default;
        public static WB_Manage_Base Default
        {
            get
            {
                if (_default == null || _default.IsDisposed)
                {
                    _default = new WB_Manage_Base();
                }
                return _default;
            }
        }


        string  EditWB_ID = "";
        string EditWBS_ID = "";
        string EditWBS_SynonymWords = "";


        WordBankBLL wordbankbll = new WordBankBLL();
        WordBankKeyWordsBLL wordbankkeywordsbll = new WordBankKeyWordsBLL();
        WordBankSynonymBLL wordbanksynonymbll = new WordBankSynonymBLL();

        private void WB_Manage_Base_Load(object sender, EventArgs e)
        {
            ShowWordBank();
           
        }

        #region 词库左侧
        /// <summary>
        /// 显示
        /// </summary>
        public void ShowWordBank()
        {
            dataPagerMain.ChangePageNumber(1);

        }
       /// <summary>
       /// 显示（下一页）
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
        private void dataPagerMain_PageNumberChanging(object sender, Controls.DataPagerPageIndexChangingEventArgs e)
        {

            var startRowIndex = e.StartRowIndex;
            var maximumRows = e.MaximumRows;
            var endRowIndex = e.StartRowIndex + e.MaximumRows;
            DataTable dt = wordbankbll.GetList();

            if (dt.Rows.Count > 0)
            {
                e.TotalCount = dt.Rows.Count;

                DataTable newdt = new DataTable();
                newdt = dt.Clone();
                DataRow[] dr = dt.Select("showindex>" + startRowIndex + " and showindex<=" + endRowIndex);
                for (int i = 0; i < dr.Length; i++)
                {
                    newdt.ImportRow((DataRow)dr[i]);
                }

                GV_WordBook.DataSource = newdt;
            }
            else
            {
                GV_WordBook.DataSource = new DataTable();
            }
        }
        /// <summary>
        /// 添加词库
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_Add_Click(object sender, EventArgs e)
        {
            WB_AddWordBook dialog_addwordbook = new WB_AddWordBook();
            dialog_addwordbook.Show();
        }


        /// <summary>
        /// 删除词库
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_Delete_Click(object sender, EventArgs e)
        {
            string DeleteIR_ID = "";
            if (MessageBox.Show("确认要删除数据吗？", "此删除不可恢复", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                try
                {
                    for (int i = 0; i <= GV_WordBook.Rows.Count - 1; i++)
                    {
                        DataGridViewCheckBoxCell check = (DataGridViewCheckBoxCell)GV_WordBook.Rows[i].Cells["Column1"];
                        if (Convert.ToBoolean(check.Value) == true)
                        {
                            DeleteIR_ID = GV_WordBook.Rows[i].Cells["WB_ID"].Value.ToString();
                            if (DeleteIR_ID != "")
                            {

                                wordbankbll.Delete(DeleteIR_ID);
                            }

                        }
                    }
                    MessageBox.Show("删除成功!");
                }
                catch (Exception error)
                {
                    MessageBox.Show("删除失败!");
                }
                ShowWordBank();
            }

        }
        /// <summary>
        /// 修改词库
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_Update_Click(object sender, EventArgs e)
        {
            WB_AddWordBook dialog_addwordbook = new WB_AddWordBook();
            for (int i = 0; i <= GV_WordBook.Rows.Count - 1; i++)
            {
                DataGridViewCheckBoxCell check = (DataGridViewCheckBoxCell)GV_WordBook.Rows[i].Cells["Column1"];
                if (Convert.ToBoolean(check.Value) == true)
                {
                    dialog_addwordbook.publicWB_ID = GV_WordBook.Rows[i].Cells["WB_ID"].Value.ToString();
                    dialog_addwordbook.publicWB_Name = GV_WordBook.Rows[i].Cells["WB_Name"].Value.ToString();
                    dialog_addwordbook.publicHeadWordId = GV_WordBook.Rows[i].Cells["HeadWordIds"].Value.ToString();
                    break;
                }
            }
            
            dialog_addwordbook.Show();
        }

        #endregion 

        #region 词库右侧
        private void GV_WordBook_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
           // int WB_ID = GV_WordBook.Rows[i].Cells["WB_ID"].Value.ToString();

             EditWB_ID = GV_WordBook.Rows[e.RowIndex].Cells["WB_ID"].Value.ToString();
             richTextBox1.Text=  wordbankkeywordsbll.GetList_WB_ID(EditWB_ID);
        }
        #region 定位查找
        //当前查询关键字的顺序
        int NowLocateCount = 0;
        /// <summary>
        /// 功能按钮，在该按钮左侧文本框中输入查询词，点击定位标记选中词库中的第一个结果词，再次点击定位下一个结果词，以此类推，直到定位到最后一个结果词为止；
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LocateWordBtn_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectionStart = 0;
            richTextBox1.SelectionLength = richTextBox1.Text.Length;
            richTextBox1.SelectionBackColor = Color.White;
            System.Text.RegularExpressions.MatchCollection mc;
            System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(SelectWord.Text.Trim()); //定义一个Regex对象实例
            mc = r.Matches(richTextBox1.Text);
            if (NowLocateCount < mc.Count)
            {
                int start = mc[NowLocateCount].Index; //记录匹配字符的位置
                int length = (SelectWord.Text.Trim()).Length;
                richTextBox1.SelectionStart = start;
                richTextBox1.SelectionLength = length;
                richTextBox1.SelectionBackColor = Color.Red;
                richTextBox1.ScrollToCaret();
                NowLocateCount++;
            }
        }
        /// <summary>
        /// 查询关键字变化，重新查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SelectWord_TextChanged(object sender, EventArgs e)
        {
            NowLocateCount = 0;
        }
        #endregion

        private void EditWBKeyWords_Click(object sender, EventArgs e)
        {

            WB_EditWBKeyWords wb_editwbkeywords = WB_EditWBKeyWords.Default;
            wb_editwbkeywords.WB_ID = EditWB_ID;
            wb_editwbkeywords.Show();
        }
        #endregion

      

      



    }
}

    

